<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;

class Role extends Admincontroller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $result = Db::query('select u.id , u.status ,u.name uname , r.name rname from xo_admin_users as u ,xo_admin_role as r , xo_admin_users_role as ur where u.id= ur.uid  and ur.rid= r.id');

        if($result){
            return view('index',['result'=>$result]);
        }else{
            return $this->success('用户列表为空,请先添加',url('admin/index/index'));
        }
    }

    //修改用户角色
    public function up()
    {
        $id=input('get.id');
        $name=input('get.num');
        var_dump($id,$name);die;
        $root = Session::get('admin');
        $list = Db::name('xo_admin_users')
                    ->alias('u')
                    ->where('u.name',$root['name'])
                    ->field('r.name')
                    ->join('xo_admin_users_role ur','ur.uid = u.id')
                    ->join('xo_admin_role r','r.id = ur.rid')
                    ->select();
        
        // 判断一下 是不是要修改为root
        // 若是 则返回error页面
        if ($list[0]['name'] == 'root') {
            return $this->error('此用户无法修改,请联系工作人员进行操作');
        }
        $role=Db::table('xo_admin_users_role')->field('rid')->select();
        if ($name == 'root') {
            return $this->error('root 有且只能有一个哦');
        }
        $name=Db::table('xo_admin_role')->field('id')->where(['name'=>$name])->find();
        $num=$name['id'];
        $rid=Db::query("select rid from xo_admin_users_role where uid=$id");
        $rid=$rid[0]['rid'];
        if($rid==$num){
            return $this->error("修改失败,该用户已是该角色",url('admin/role/index'));
        }

        $result = Db('xo_admin_users_role')->where(['uid'=>$id])->update(['rid'=>$num]);
        if($result>0)
        {
            return $this->success('修改成功',url('admin/role/index'));
        }else{
            return $this->error('修改失败',url('admin/role/index'));
        }

    }


//查询指定用户的角色 控制器和方法权限
    public function action($id)
    {
        $data=DB::query('select  u.name uname , r.name rname, r.id from xo_admin_users as u ,xo_admin_role as r , xo_admin_users_role as ur where u.id= ur.uid  and ur.rid= r.id and u.id='.$id);
        if (empty($data)) {
            return $this->error('页面开小差去啦!~~~');
        }
        //得到用户名  用户名有的角色  角色ID
        $uname=$data[0]['uname'];
        $rname=$data[0]['rname'];
        $rid=$data[0]['id'];
        // var_dump($rname);
        if ($rname == 'root') {
            $res=Db::table('xo_admin_pp')->field('controller,method')->select();
        }else{
            
            $res=DB::query('select p.controller , p.method  from xo_admin_role_pp as r , xo_admin_pp as p where r.rid='. $rid.' and r.pid=p.id ');
        }
        //查出的结果
        foreach($res as $k =>$v){
            $list[]=$v;
               $controller[]=$v['controller'];
               $method[]=$v['method'];
        }
        //得到控制器和方法
        //dump($list);
        $info['uname']=$uname;
        $info['rname']=$rname;
        $info['id']=$id;
        // dump($list);
        // dump($info);die;
        return view('role/action',['list'=>$list,'info'=>$info]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if(!Request::instance()->isAjax()){
            return $this->error('非法请求',url('admin/user/user'));
        }
        $res = Db::query("select u.icon, u.name uname , r.name rname from xo_admin_users as u ,xo_admin_role as r , xo_admin_users_role as ur where u.id= ur.uid  and ur.rid= r.id and u.id=$id");
        if($res===null){
            $data['status']=false;
            $data['title']='查无数据';
            $data['info']='查无数据,请重试';
            $data['src']='/static/admin/images/error.jpg';
        }else{
            $data['status']=true;
            $data['title']='用户['.$res[0]['uname'].']的信息';
            $data['num']=$id;
            $data['info']=$res;
        }

        $list=DB::table('xo_admin_role')->field('name')->select();

        $data['role']=$list;
        return json($data);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {
        if(!Request::instance()->isAjax()){
            return $this->error('非法请求',url('admin/index/index'));
        }




    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
